'use strict';
module.exports = function (grunt) {
    require('time-grunt')(grunt);//显示任务执行时间
    /*基础配置*/
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        /* **********监听的配置 ********** */
        watch: {
            jade: {//监听jade文件的变化
                files: ['views/**'],
                options: {
                    livereload: true
                }
            },
            js: {//监听js文件的变化
                files: ['public/src/js/**', 'mongo/**/**/*.js'],
                tasks: ['jshint', 'uglify'],
                options: {
                    livereload: true
                }
            },
            css: {//监听css文件的变化
                files: ['public/src/css/*.css'],
                tasks: ['cssmin'],
                options: {
                    nospawn: true
                }
            },
            less: {//监听less的变化,有变化启动less任务
                files: ['public/src/less/*.less'],
                tasks: ['less'],
                options: {
                    nospawn: true
                }
            },
            bower: {
                files: ['bower_components/*'],
                tasks: ['wiredep']
            }
        },
        /* --------------------------End-------------------------------*/
        /*具体插件执行任务的配置*/
        jshint: {//代码检查插件
            options: {
                jshintrc: '.jshintrc',
                ignores: ['public/libs/**/*.js']
            },
            all: ['public/src/js/*.js']
        },
        less: {//less编译插件
            development: {
                options: {
                    compress: true,
                    yuicompress: true,
                    optimization: 2
                },
                files: {
                    'public/src/css/less.css': 'public/src/less/less.less'
                }
            }
        },
        cssmin: {//css压缩合并
            options: {
                report: 'gzip'
            },
            development: {
                files: [{
                    expand: true,
                    cwd: 'public/src/css',
                    src: ['*.css', '!*.min.css'],
                    dest: 'public/dist/css',
                    ext: '.min.css'
                }]
            }
        },
        imagemin: {//图片压缩
            options: {
                report: 'gzip'
            },
            development: {
                files: [{
                    expand: true,
                    cwd: 'public/src/images',
                    src: '*.{png,jpg,jpeg,gif,webp,svg}',
                    dest: 'public/dist/images',
                }]
            }
        },
        uglify: {//js压缩插件
            options: {
                banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n',//添加banner
                mangle: true
            },
            development: {
                files: {
                    'public/dist/js/admin.min.js': 'public/src/js/admin.js',
                    'public/dist/js/detail.min.js': 'public/src/js/detail.js'
                }
            }
        },
        clean: {
            development: {
                src: 'public/dist/**/*',
                filter: 'isFile'
            }
        },
        nodemon: {//热启动插件(为什么要这个)
            development: {
                options: {
                    file: 'app.js',
                    args: [],
                    ignoredFiles: ['README.md', 'node_modules/**', '.DS_Store'],
                    watchedExtensions: ['js'],
                    watchedFolders: ['./'],
                    debug: true,
                    delayTime: 1,
                    env: {
                        PORT: 3000
                    },
                    cwd: __dirname
                }
            }
        },
        wiredep: {//写入静态资源
            development: {
				ignorePath: /^(\.\.\/)*\.\.\/public/,
                src: 'views/includes/head.jade',   // .jade support...
                options: {
                    // https://github.com/taptapship/wiredep#configuration
                }
            }
        },
        concurrent: {//并行
            tasks: ['nodemon', 'watch', 'uglify', 'cssmin', 'imagesmin', 'wiredep']
        },
        options: {
            logConcurrentOutput: true
        }
    })
    /*配置起作用的工具*/
    grunt.loadNpmTasks('grunt-contrib-watch')
    grunt.loadNpmTasks('grunt-contrib-less')
    grunt.loadNpmTasks('grunt-contrib-uglify')
    grunt.loadNpmTasks('grunt-contrib-jshint')
    grunt.loadNpmTasks('grunt-contrib-clean')
    grunt.loadNpmTasks('grunt-contrib-cssmin')
    grunt.loadNpmTasks('grunt-contrib-imagemin')
    grunt.loadNpmTasks('grunt-wiredep')
    grunt.loadNpmTasks('grunt-nodemon')
    grunt.loadNpmTasks('grunt-concurrent')
    grunt.option('force', true)
    /*配置任务*/
    grunt.registerTask('default', ['clean', 'concurrent'])

}